package org.jeecg.modules.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.flower.entity.FlowerBoxes;
import org.jeecg.modules.flower.entity.FlowerBoxesDetal;
import org.jeecg.modules.flower.mapper.FlowerBoxesDetalMapper;
import org.jeecg.modules.flower.mapper.FlowerBoxesMapper;
import org.jeecg.modules.flower.service.IFlowerBoxesService;
import org.jeecg.modules.flower.util.CommonUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 采后装箱管理
 * @Author: jeecg-boot
 * @Date:   2022-11-21
 * @Version: V1.0
 */
@Service
public class FlowerBoxesServiceImpl extends ServiceImpl<FlowerBoxesMapper, FlowerBoxes> implements IFlowerBoxesService {

	@Autowired
	private FlowerBoxesMapper flowerBoxesMapper;
	@Autowired
	private FlowerBoxesDetalMapper flowerBoxesDetalMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveMain(FlowerBoxes flowerBoxes, List<FlowerBoxesDetal> flowerBoxesDetalList) {
		String mainid = UuidUtils.getUUID();
		int sumNum = 0;
		if(flowerBoxesDetalList!=null && flowerBoxesDetalList.size()>0) {
			for(FlowerBoxesDetal entity:flowerBoxesDetalList) {
				if (StringUtils.isNotBlank(entity.getPrintId()) && !"手动打标签".equals(entity.getPrintId())) {
					QueryWrapper<FlowerBoxesDetal> queryWrapper = new QueryWrapper<>();
					queryWrapper.eq("print_id", entity.getPrintId());
					List<FlowerBoxesDetal> flowerBoxesDetals = flowerBoxesDetalMapper.selectList(queryWrapper);
					if (flowerBoxesDetals.size() > 0) {
						throw new RuntimeException(entity.getPrintId() + "-->标签已被装箱了，请检查！");
					}
				}
				if(StringUtils.isBlank(entity.getPrintId())){
					entity.setPrintId("手动打标签");
				}
				if(StringUtils.isBlank(entity.getVarietyCode())){
					entity.setVarietyCode("手动打标签");
				}if(StringUtils.isBlank(entity.getStorehouse())){
					entity.setStorehouse("手动打标签");
				}if(StringUtils.isBlank(entity.getCost())){
					entity.setCost("手动打标签");
				}

				//外键设置
				entity.setMainid( mainid );
				flowerBoxesDetalMapper.insert(entity);
				sumNum += entity.getNumber();
			}
		}
		flowerBoxes.setId(mainid);
		flowerBoxes.setNumber( sumNum);
		//生成单号
		//自动生成出库单号
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String dateNo = simpleDateFormat.format(new Date());
		String refex = "XCK" ;
		String refex2 = refex + dateNo;
		LambdaQueryWrapper<FlowerBoxes> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.like(FlowerBoxes::getBoxno, refex2);
		lambdaQueryWrapper.orderByDesc(FlowerBoxes::getBoxno);
		List<FlowerBoxes> list = this.list(lambdaQueryWrapper);
		String oldNo = "";
		if (list.size() > 0) {
			oldNo = list.get(0).getBoxno();
		}
		String newNo = CommonUtil.getNo(refex, oldNo, 3);
		flowerBoxes.setBoxno(newNo);
		//自动生成出库单号----end----
		flowerBoxesMapper.insert(flowerBoxes);
		return newNo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(FlowerBoxes flowerBoxes,List<FlowerBoxesDetal> flowerBoxesDetalList) {
		//1.先删除子表数据
		flowerBoxesDetalMapper.deleteByMainId(flowerBoxes.getId());
		int sumNum = 0;
		//2.子表数据重新插入
		if(flowerBoxesDetalList!=null && flowerBoxesDetalList.size()>0) {
			for(FlowerBoxesDetal entity:flowerBoxesDetalList) {
				//外键设置
				entity.setMainid(flowerBoxes.getId());
				flowerBoxesDetalMapper.insert(entity);

				sumNum += entity.getNumber();
			}
		}
		flowerBoxes.setNumber( sumNum);
		flowerBoxesMapper.updateById(flowerBoxes);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		FlowerBoxes flowerBoxes =new FlowerBoxes();
		flowerBoxes.setState(-2);
		flowerBoxesMapper.updateById(flowerBoxes);
//		flowerBoxesDetalMapper.deleteByMainId(id);
//		flowerBoxesMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			FlowerBoxes flowerBoxes =new FlowerBoxes();
			flowerBoxes.setState(-2);
			flowerBoxesMapper.updateById(flowerBoxes);
//			flowerBoxesDetalMapper.deleteByMainId(id.toString());
//			flowerBoxesMapper.deleteById(id);
		}
	}
	
}
